import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test_project/module/inherited_test_page/share_date_widget.dart';

import 'inherited_test_sub_widget.dart';

class InheritedTestPage extends StatefulWidget {
  @override
  State createState() => new _InheritedTestPageState();
}

class _InheritedTestPageState extends State<InheritedTestPage> {
  int count = 0;

  @override
  Widget build(BuildContext context) {
    return  Center(
      child: ShareDataWidget( //使用ShareDataWidget
        data: count,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(bottom: 20.0),
              child: InheritedTestSubWidget(),//子widget中依赖ShareDataWidget
            ),
            RaisedButton(
              child: Text("Increment"),
              //每点击一次，将count自增，然后重新build,ShareDataWidget的data将被更新
              onPressed: () => setState(() => ++count),
            ),
            RaisedButton(
              padding: EdgeInsets.symmetric(vertical: 10),
              child: Text("print share data widget"),
              //每点击一次，将count自增，然后重新build,ShareDataWidget的data将被更新
              onPressed: (){
                final shareDataWidget =  ShareDataWidget.of(context);
                print("share data widget is ${shareDataWidget == null ? "empty" : "not empty"}");
                // final dataString = ShareDataWidget.of(context).data.toString();
              },
            ),
          ],
        ),
      ),
    );
  }
}

